package EA;
import EA.recording.FitEvalCountListener;

public class Fitness
{

  /**
     A listener to be alerted every time the fitness function has
     been evaluated <tt>fitevalsamplerate</tt> times. Can be used to
     collect data depending on how many times the fitness have been
     evaluated. 
     @see EA.recording.FitEvalCountListener
     @see EA.testproblems.NumericalProblem#setCountListener
  */
  public FitEvalCountListener countlistener = null;

  /** The sample rate for sampling. */
  public int fitevalsamplerate = 0;
  
  /** The number of evaluations of the fitnessfunction. */
  public int currentevals = 0;
  
  public Fitness()
    {
      super();
    }

  /** 
      Calculate the fitness. The reason why this is a final method is
      because you have to use override Fitness_calcFitness_inner()
      instead.
  */
  public final double calcFitness()
    {
      if (countlistener!=null && fitevalsamplerate>0) {
	currentevals++;
	if (currentevals%fitevalsamplerate ==0) {
	  countlistener.fitnessEvaluationsCounted(currentevals);
	}
      }
      return Fitness_calcFitness_inner();
    }

  public double Fitness_calcFitness_inner()
    {
      return 0;
    }

  /** Set a countlistener for this fitnessfunction. */
  public void setCountListener(FitEvalCountListener listener, int samplerate) 
    {
      countlistener = listener;
      fitevalsamplerate = samplerate;
    }

  /** Reset the evalcounter <tt>currentevals</tt>. */
  public void resetFunction()
    {
      currentevals = 0;
    }



}
